package com.iphanta.community.following.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iphanta.community.common.BusinessException;
import com.iphanta.community.common.utils.ResultUtils;
import com.iphanta.community.following.mapper.CommentsMapper;
import com.iphanta.community.following.mapper.DynamicMapper;
import com.iphanta.community.following.service.AdminCommentsService;
import com.iphanta.community.following.traffic.admin.AdminDeleteCommentRequest;
import com.iphanta.community.following.traffic.admin.AdminDeleteCommentResponse;
import com.iphanta.community.following.traffic.admin.AdminQueryCommentsRequest;
import com.iphanta.community.following.traffic.admin.AdminQueryCommentsResponse;
import com.iphanta.community.following.traffic.admin.vo.AdminCommentItem;
import com.iphanta.community.following.traffic.admin.vo.AdminDynamicItem;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author 墨声 E-mail: zehong.hongframe.huang@gmail.com
 * create time: 4/24/22 4:35 PM
 */
@Service
public class AdminCommentsServiceImpl implements AdminCommentsService {

    @Autowired
    private CommentsMapper commentsMapper;

    @Value("${admin.comments.query.range}")
    private Integer range;


    @Override
    public AdminQueryCommentsResponse query(AdminQueryCommentsRequest request) {
        if (Objects.nonNull(request.getStart()) || Objects.nonNull(request.getStop())) {
            if (!(Objects.nonNull(request.getStart()) && Objects.nonNull(request.getStop()))) {
                throw new BusinessException("非法区间", ResultUtils.FAIL_ON_BUSINESS.result());
            }
        }

        Boolean timeRange = false;
        String start;
        String stop = null;

        if (Objects.nonNull(request.getStart()) && Objects.nonNull(request.getStop())) {
            if (request.getStart().getTime() > request.getStop().getTime()) {
                throw new BusinessException("开始时间不得大于结束时间", ResultUtils.FAIL_ON_BUSINESS.result());
            }
            int diff = differentDaysByMillisecond(request.getStart(), request.getStop());
            if (diff > range) {
                throw new BusinessException("时间区间不得大于" + range, ResultUtils.FAIL_ON_BUSINESS.result());
            }
            timeRange = true;
            start = DateFormatUtils.format(request.getStart(), "yyyy-MM-dd");
            stop = DateFormatUtils.format(request.getStop(), "yyyy-MM-dd");
        } else {
            Instant instant = Instant.now().minus(range, ChronoUnit.DAYS);
            start = DateFormatUtils.format(instant.toEpochMilli(), "yyyy-MM-dd");
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<AdminCommentItem> list = this.commentsMapper
                .query(request.getNickname(), request.getComment(), start, stop, timeRange);
        PageInfo<AdminCommentItem> pageInfo = new PageInfo<>(list);

        AdminQueryCommentsResponse response = new AdminQueryCommentsResponse();
        response.setPageNum(pageInfo.getPageNum());
        response.setPageSize(pageInfo.getPageSize());
        response.setTotal(pageInfo.getTotal());
        response.setLastPage(pageInfo.getLastPage());
        response.setList(list);
        return response;
    }

    @Override
    public AdminDeleteCommentResponse delete(AdminDeleteCommentRequest request) {
        this.commentsMapper.deleteComments(request.getCommentId());
        AdminDeleteCommentResponse response = new AdminDeleteCommentResponse();
        response.setSuccess(true);
        return response;
    }

    public static int differentDaysByMillisecond(Date start, Date stop) {
        int days = (int) ((stop.getTime() - start.getTime()) / (1000 * 3600 * 24));
        return days;
    }
}
